 // @ 516
// _(:з」∠)_

define([
    'pixi-2D',
    'colorMatch',
    'leaflet-PixiOverlay'
], function() {
    "use strict";
    
    let multiFacet = {
        colorRGB2Hex: function( color ) { // rgb 转 16进制 - 颜色
            let _this = this,
                rgb = color.split(','),
                r = parseInt(rgb[0].split('(')[1]),
                g = parseInt(rgb[1]),
                b = parseInt(rgb[2].split(')')[0]);
        
            let hex = "0x" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
            return hex;
        },
        drawGrid: function( filePath, param ) {
            let _this = this;

            RST.basic( filePath, param, function( result ) {

                // let top = 42.7,
                //     left = 113.56,
                //     right = 122.8,
                //     botton = 29.58,
                //     deltaX = 0.01,
                //     deltaY = 0.01,
                //     polygonLatLngs = [];

                // for ( let x = 0; x < result[0].rows; x++ ) {
                //     polygonLatLngs[x] = [];
                //     for ( let y = 0; y < result[0].cols; y++ ) {
                //         let lat = top - ( x * deltaY ) - deltaY,
                //             lng = left + ( y * deltaX ) + deltaX,
                //             latx = Math.round( lat * 100 ) / 100,
                //             lngx = Math.round( lng * 100 ) / 100,
                //             latLngs = [
                //                 [ lat + deltaX, lng - deltaX ],
                //                 [ lat + deltaX, lng ],
                //                 [ lat, lng ],
                //                 [ lat, lng - deltaX ],
                //                 [ lat + deltaX, lng - deltaX ]
                //             ];
                //         polygonLatLngs[x].push( latLngs );
                //     }
                // }
        
                let projectedPolygonArry = [],
                    pixiContainer = new PIXI.Container(),
                    firstDraw = true,
                    legendArry = [],
                    prevZoom;

                if ( MC.mapLayer.GridLayer ) MC.mapLayer.GridLayer = null;

                MC.mapLayer["GridLayer"] = L.pixiOverlay(function( utils ) {
                    let zoom = utils.getMap().getZoom(),
                        container = utils.getContainer(),
                        renderer = utils.getRenderer(),
                        project = utils.latLngToLayerPoint,
                        scale = utils.getScale();
        
                    if ( firstDraw ) {
                        let polygonLatLngs = [];
                        for ( let x = 0; x < 924; x++ ) {
                            polygonLatLngs[x] = [];
                            for ( let y = 0; y < 1311; y++ ) {
                                let latLng = MC.customCRS.unproject( L.point( ( 2215332 - ( x * 1000 ) ), ( 3715762 + ( y * 1000 ) ) ) );
                                polygonLatLngs[x].push( [ latLng.lat, latLng.lng ] );
                            }
                        }

                        for ( let x = 0; x < polygonLatLngs.length; x++ ) {
                            projectedPolygonArry[x] = [];
                            polygonLatLngs[x].map(function( coords ) {
                                projectedPolygonArry[x].push( project( coords ) );
                            });
                        }
                    }

                    if ( firstDraw || prevZoom !== zoom ) {
                        pixiContainer.removeChildren( 0, 924 * 1311 );
        
                        for ( let x = 0; x < projectedPolygonArry.length; x++ ) {
                            projectedPolygonArry[x].forEach(function( coords, index ) {
                                let polygon = new PIXI.Graphics(),
                                    colorHex = "";
                                
                                // let colorArry = colorMatch[ result[0].element ];
                                let colorArry = colorMatch[ "TMP" ];

                                for ( let z = 0; z < colorArry.length; z++ ) {
                                    let val = 13,
                                        startVal = 12,
                                        endVal = 14,
                                        startColor = "rgb( 241,147,3 )";
        
                                    if ( val >= startVal && val < endVal ) {
                                        colorHex = _this.colorRGB2Hex( startColor );
                                        if ( val >= 22 && val <= (-2.2) ) {
                                            legendArry.push( colorArry[z] );
                                        }
                                        break;
                                    }
                                }
        
                                polygon.beginFill( colorHex, 0.7 );
                                colorHex = null;

                                polygon.drawRect( coords.x, coords.y, 5, 5 );

                                // if ( index == 0 ) {
                                //     polygon.moveTo( coords.x, coords.y );
                                // } else {
                                //     polygon.lineTo( coords.x, coords.y );
                                // }
                                
                                polygon.endFill();
                                pixiContainer.addChild( polygon );
                                polygon = null;
                            });
                        }
                    }
                    firstDraw = false;
                    prevZoom = zoom;
                    renderer.render( container );

                }, pixiContainer);
        
                MC.mapLayer.GridLayer.addTo( MC.map );

                let heavy = objToHeavy( legendArry, "start" ),
                    sortArry = heavy.sort( objCompare( "start" ) );
                Legend.elmt( sortArry );
        
            });
        }
    };

    window.multiFacet = multiFacet;

    return multiFacet;
});